/**
 * 列表页通用方法
 */

layui.define(["jquery","upload"], function (exports) {
    var $ = layui.$,
        upload = layui.upload,
        layer = layui.layer;

    var miniUpload = {

        /**
         * 上传单个图片
         *
         * @param elem 上传按钮
         * @param elemDemo 上传图片预览
         */
        uploadImg : function (elem) {
            var uploadImg =  upload.render({
                elem: "#" + elem,
                url: "/admin/upload/img",
                method:'POST',
                done:function (res) {
                    //如果上传失败
                    if(res.code === 200){
                        layer.msg(res.msg)
                        $('input[name='+ elem +']').val(res.save_path)
                    }else{
                        return layer.msg('上传失败');
                    }
                },
                error: function(){
                    //演示失败状态，并实现重传
                    var demoText = $('#demo_text');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function(){
                        uploadImg.upload();
                    });
                }
            })
        },

        /**
         * 上传多个图片
         *
         * @param elem
         * @param elemDemo
         */
        uploadImages : function (elem, elemDemo) {
            upload.render({
                elem: '#' + elem
                ,url: '/admin/upload/img' //改成您自己的上传接口
                ,multiple: true
                ,done: function(res){
                    var banner = $('input[name='+ elem +']');
                    if(res.code === 200){
                        if (banner.val() === '') {
                            banner.val(res.save_path);
                        } else {
                            banner.val(banner.val() + ',' + res.save_path);
                        }

                        $('#'+elemDemo).append(' <span style="position: relative;width: 200px;height: 200px" class="imgs">\n' +
                            '<img style="width: 200px;" src="'+res.save_path+'" alt="" class="layui-upload-img">\n' +
                            '<i class="layui-icon" title="移除图片" style="color: #ff0000;font-size: 60px; position: absolute;top:0 ; right: 40%;cursor: pointer;  ">&#xe640;</i>\n' + ' </span>');

                       miniUpload.deleteImg(elemDemo)
                    }else{
                        return layer.msg('上传失败');
                    }
                }
            });
            miniUpload.deleteImg(elem, elemDemo)
        },

        /**
         * 多图列表删除单张图片
         *
         * @param elemDemo
         */
        deleteImg: function (elem, elemDemo) {
            // 删除图片
            $('.imgs i.layui-icon').unbind('click').on("click", function (e) {
                var banner = $('input[name='+ elem +']');
                $(this).parent().remove();
                console.log(banner)
                var preview_arr = [];
                $('#'+ elemDemo + ' img').each(function (i, v) {
                    preview_arr.push($(v).attr('src'))
                });
                console.log(banner)
                banner.val(preview_arr.join(','));
            });
        },

        /**
         * 上传文件
         *
         * @param elem
         * @param elemDemo
         */
        uploadFile:function (elem,elemDemo){
            var uploadFile = upload.render({
                elem: "#" + elem,
                url: "/admin/upload/file",
                method:'POST',
                accept:'file',
                before:function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result) {
                        $('#'+elemDemo).attr('src', result); //链接（base64）
                    })
                },
                done:function (res) {
                    //如果上传失败
                    if(res.code === 200){
                        layer.msg(res.msg)
                        $('input[name='+ elem +']').val(res.save_path)
                    }else{
                        return layer.msg('上传失败');
                    }
                },
                error: function(){
                    //演示失败状态，并实现重传
                    var demoText = $('#demo_text');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function(){
                        uploadImg.upload();
                    });
                }

            })
        }
    };

    exports("miniUpload", miniUpload);
});
